package mars;

import mars.Position.Heading;


public class InputParser {
	
	private static InputParser inputParser = null;
	
	private InputParser() {
		
	}
	
	public static InputParser getInstance() {
		if(inputParser == null) {
			inputParser = new InputParser();
		}
		return inputParser;
	}

	public Position parsePositionString(String initialRoverString) throws ParseException {
		String[] tokenString = initialRoverString.split("\\s+");
		Position position = null;
		if(tokenString.length != 3) {
			throw new ParseException("Initial rover position not in correct format");
		}
		try {
			int x = Integer.parseInt(tokenString[0]);
			int y = Integer.parseInt(tokenString[1]);
			Heading heading = null;
			switch(tokenString[2].toUpperCase().charAt(0)) {
			case 'N' : heading = Heading.N;
				break;
			case 'S' : heading = Heading.S;
				break;
			case 'E' : heading = Heading.E;
				break;
			case 'W' : heading = Heading.W;
				break;
			default : throw new ParseException("Heading need to be N, E, S or W");
			}
			position = new Position(x, y, heading);
		} catch(NumberFormatException e) {
			throw new ParseException("Position needs be the integer coordinates followed by heading"); 
		}
		return position;
	}
	
	public void definePlateu(String plateauCoord) throws ParseException {
		String[] boundsString = plateauCoord.split("\\s+");
		if(boundsString.length != 2) {
			throw new ParseException("Can't split plateau coordinates. Please check "+plateauCoord);
		}
		try {
			int x = Integer.parseInt(boundsString[0]);
			int y = Integer.parseInt(boundsString[1]);
			Plateu.getInstance().setBottomLeft(new Position(0, 0));
			Plateu.getInstance().setTopRight(new Position(x, y));
		}catch(NumberFormatException e) {
			throw new ParseException("Plateau coordinates need to be 2 integers with a space ");
		}
	}
	
}
